package com.jjclub.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jjclub.entity.JjclubMenu;
import com.jjclub.entity.JjclubRole;
import com.jjclub.entity.JjclubRoleMenu;
import com.jjclub.entity.JjclubUser;
import com.jjclub.mapper.JjclubUserMapper;
import com.jjclub.mapper.util.BatchMapper;
import com.jjclub.service.MenuService;
import com.jjclub.service.Test01Service;
import com.jjclub.util.CommonUtil;
import com.jjclub.vo.ResponseVO;
import com.sun.org.apache.xpath.internal.operations.Mod;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Classname MenuController
 * @Description TODO
 * @Date 2019/11/11 9:34
 * @author jq
 */
@Controller
@RequestMapping("/menuAction")
public class MenuController {


    @Autowired
    private MenuService menuService;


    @Autowired
    private Test01Service test01Service;


    @Autowired
    BatchMapper  batchMapper;

    /**
     * description:返回下拉树页面
     * @param: [model]
     * @author: jj
     * @date: 2019/11/11 9:35
     * @return java.lang.String
     **/
    @GetMapping("menuRole")
    public String showMenuRole(Model model){
        return "service/menurole";
    }
    /**
     * description:返回下拉树页面
     * @param: [model]
     * @author: jj
     * @date: 2019/11/11 9:35
     * @return java.lang.String
     **/
    @GetMapping("menuList")
    public String showMenuList(Model model){
        return "service/menu/menulist";
    }

    /**
     * description:打开菜单添加页面
     * @param: []
     * @author: jj
     * @date: 2019/11/14 9:29
     * @return java.lang.String
     **/
    @GetMapping("menuAdd/{id}")
    public String addMenu(@PathVariable String id,Model model){
        model.addAttribute("pid",id);
        return "service/menu/add";
    }
    @ApiOperation(value = "菜单操作",notes = "添加操作")
    @PostMapping("menuAdd")
    @ResponseBody
    public ResponseVO addMenu(JjclubMenu menu){
        ResponseVO responseVO = menuService.addMenu(menu);
        return responseVO;
    }

    @ApiOperation(value = "菜单操作",notes = "删除操作")
    @DeleteMapping("menuDel/{id}")
    @ResponseBody
    public ResponseVO delMenu(@PathVariable("id") String id){
        ResponseVO responseVO = menuService.delMenu(id);
        return responseVO;
    }

    @GetMapping("menuEdit/{id}")
    public String showMenuEdit(@PathVariable("id")String id, Model model){
        JjclubMenu jjclubMenu = menuService.getJjclubMenuByid(id);
        model.addAttribute("menu",jjclubMenu);
        return "service/menu/edit";
    }

    @ApiOperation(value = "菜单操作",notes = "编辑菜单")
    @PostMapping("menuEdit")
    @ResponseBody
    public  ResponseVO MenuEdit(JjclubMenu menu){
        ResponseVO responseVO = menuService.updateMenu(menu);
        return responseVO;
    }

    /**
     * description:
     * @param: []
     * @author: jj
     * @date: 2019/11/14 9:32
     * @return java.lang.String
     **/
    @ApiOperation(value = "菜单操作" ,  notes="获取菜单json列表")
    @GetMapping("menuListJson")
    @ResponseBody
    public ResponseVO menuListJson()  {
        ResponseVO responseVO = new ResponseVO();
        List<JjclubMenu> list = menuService.getAllMenus();
        responseVO.setCode(0);
        responseVO.setMsg("操作成功");
        responseVO.setData(list);
        return responseVO;
    }

    @GetMapping("menuTreeList/{roleid}")
    @ResponseBody
    public JSONArray menuTreeList(@PathVariable("roleid")String id){
        List<JjclubMenu> list = menuService.getAllMenus();
        List<JjclubRoleMenu> jjclubRoleMenus = menuService.getmenusByroleid(id);
        List<String> strings =  jjclubRoleMenuListToStringList(jjclubRoleMenus);
        JSONArray jsonArray = new JSONArray();
        for (JjclubMenu jjclubMenu : list){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id",jjclubMenu.getId());
            jsonObject.put("name",jjclubMenu.getName());
            jsonObject.put("pId",jjclubMenu.getParentid());
            if (strings.contains(jjclubMenu.getId().toString())){
                jsonObject.put("checked",true);
            }
            jsonArray.add(jsonObject);
        }
        return jsonArray;
    }
    /**
     * description:转String集合
     * @param: [jjclubRoleMenus]
     * @author: jj
     * @date: 2019/11/18 11:18
     * @return java.util.List<java.lang.String>
     **/
    private List<String> jjclubRoleMenuListToStringList(List<JjclubRoleMenu> jjclubRoleMenus){
        List<String> resList = new ArrayList<>();
        for (JjclubRoleMenu jjclubRoleMenu:jjclubRoleMenus){
            resList.add(jjclubRoleMenu.getMenuid());
        }
        return resList;
     }
     @GetMapping("/baidu")
     public String openBaidu(){
        List<JjclubUser> list = new ArrayList<>();
         String hashAlgorithmName = "MD5";
         String credentials = "123456";
         int hashIterations = 2;
         for(int i=0;i<100000;i++){
            JjclubUser jjclubUser = new JjclubUser();
            jjclubUser.setId(CommonUtil.getKey());
            jjclubUser.setCreatetime(new Date());
            jjclubUser.setLoginid("club"+i);
             ByteSource credentialsSalt = ByteSource.Util.bytes("club"+i);
             Object obj = new SimpleHash(hashAlgorithmName, credentials, credentialsSalt, hashIterations);
            jjclubUser.setLoginname("club"+i);
            jjclubUser.setPassword(obj.toString());
            jjclubUser.setSalt("club"+i);
            jjclubUser.setStatus(1);
            list.add(jjclubUser);
         }
        batchMapper.insertBatch(list, JjclubUserMapper.class);
        return "redirect:https://www.baidu.com";
     }
}
